home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Language/OS - Multiplatform Resource Library
/
LANGUAGE OS.iso
/
simula
/
books
/
books.lha
/
kirkerud
/
arrproc.sim
< prev
next >
Wrap
Text File
|
1993-08-16
|
7KB
|
206 lines
begin
procedure Min_max_in_1dim_array(arr, low_ind, high_ind,
min_val, max_val);
name min_val, max_val;
integer array arr;
integer low_ind, high_ind, min_val, max_val;
begin
integer ind;
min_val := maxint; max_val := minint;
for ind := low_ind step 1 until high_ind do
begin
min_val := min(arr(ind), min_val);
max_val := max(arr(ind), max_val);
end;
end of Min_max_in_1dim_array;
procedure Min_max_in_2dim_array(arr, low_ind1, high_ind1,
low_ind2, high_ind2,
min_val, max_val);
name min_val, max_val;
integer array arr;
integer low_ind1, high_ind1, low_ind2, high_ind2, min_val, max_val;
begin
integer ind1, ind2;
min_val := maxint; max_val := minint;
for ind1 := low_ind1 step 1 until high_ind1 do
for ind2 := low_ind2 step 1 until high_ind2 do
begin
min_val := min(arr(ind1, ind2), min_val);
max_val := max(arr(ind1, ind2), max_val);
end;
end of Min_max_in_2dim_array;
procedure Slowsort(arr, low_ind, high_ind);
integer array arr; integer low_ind, high_ind;
begin
integer ind;
for ind := low_ind step 1 until high_ind - 1 do
Swap(arr, ind, min_ind_in_1dim_array(arr, ind, high_ind));
end of Slowsort;
procedure Swap(arr, ind1, ind2);
integer array arr; integer ind1, ind2;
if ind1 ne ind2 then
begin integer temp;
temp := arr(ind1);
arr(ind1) := arr(ind2); arr(ind2) := temp;
end of Swap;
integer procedure min_ind_in_1dim_array(arr, low_ind, high_ind);
integer array arr; integer low_ind, high_ind;
begin integer ind, min_ind;
min_ind := low_ind;
for ind := low_ind + 1 step 1 until high_ind do
if arr(ind) < arr(min_ind) then min_ind := ind;
min_ind_in_1dim_array := min_ind;
end;
procedure Merge(A, B, C, a_high, b_high, c_high);
name c_high;
integer array A, B, C;
integer a_high, b_high, c_high;
begin
integer a_ind, b_ind, c_ind, a_val, b_val;
Boolean a_finished, b_finished;
a_ind := 1; a_val := A(a_ind); a_finished := false;
b_ind := 1; b_val := B(b_ind); b_finished := false;
c_ind := 0;
while not (a_finished and b_finished) do
begin ! a_finished will be true when all elements in A have
! been merged into C, similarily with b_finished.
! Because now not both a_finished and b_finished ,
! there must be at least
! one element in A or B which have not found its place in C.
! This will be done now: ;
c_ind := c_ind + 1;
if a_val < b_val then ! This means that a_val is the element
! to be placed in C;
begin
C(c_ind) := a_val;
a_ind := a_ind + 1; ! Find the next element in A;
if a_ind le a_high then a_val := A(a_ind)
else begin a_val := maxint; a_finished := true end;
end
else begin
C(c_ind) := b_val;
b_ind := b_ind + 1;
if b_ind le b_high then b_val := B(b_ind)
else begin b_val := maxint; b_finished := true end;
end
end;
c_high := c_ind;
end of Merge;
procedure give_help;
begin
outtext("Legal commands: "); outimage;
outtext(" ? give help"); outimage;
outtext(" r: read array "); outimage;
outtext(" m: test merge "); outimage;
outtext(" s: test Slowsort "); outimage;
outtext(" x: test findminmax "); outimage;
outtext(" w: write arrays"); outimage;
outtext(" q: quit testing"); outimage;
end of give_help;
procedure read_array;
begin character arrchar;
arrchar := prompt_for_char("Read A, B or C? ");
if arrchar = 'A' then read_arr(A, na) else
if arrchar = 'B' then read_arr(B, nb)
else read_arr(C, nc);
end;
procedure read_arr(arr, narr);
name narr; integer array arr; integer narr;
begin integer ind;
narr := prompt_for_int("How many elements? ");
for ind := 1 step 1 until narr do
arr(ind) := prompt_for_int("Type an element> ");
end of read_arr;
integer procedure prompt_for_int(prompt); text prompt;
begin
outtext(prompt); breakoutimage;
inimage; prompt_for_int := inint;
end;
character procedure prompt_for_char(prompt); text prompt;
begin
outtext(prompt); breakoutimage;
inimage; prompt_for_char := inchar;
end;
procedure test_merge;
Merge(A, B, C, na, nb, nc);
procedure test_Slowsort;
begin character arrchar;
arrchar := prompt_for_char("Slowsort A, B or C? ");
if arrchar = 'A' then
begin Slowsort(A, 1, na); write_array("A", A, na) end else
if arrchar = 'B' then
begin Slowsort(B, 1, nb); write_array("B", B, nb) end else
if arrchar = 'C' then
begin Slowsort(C, 1, nc); write_array("C", C, nc) end;
end of test_Slowsort;
procedure test_findminmax;
begin character arrchar; integer min_val, max_val;
arrchar := prompt_for_char("Minmax for A, B or C? ");
if arrchar = 'A' then
Min_max_in_1dim_array(A, 1, na, min_val, max_val) else
if arrchar = 'B' then
Min_max_in_1dim_array(B, 1, nb, min_val, max_val)
else
Min_max_in_1dim_array(C, 1, nc, min_val, max_val);
outtext("Minimum in "); outchar(arrchar); outtext(": "); outint(min_val,6);
outtext(" Maximum: "); outint(max_val,6); outimage;
end of test_findminmax;
procedure write_arrays;
begin
write_array("A", A, na);
write_array("B", B, nb);
write_array("C", C, nc);
end of write_arrays;
procedure write_array(arr_name, arr, arr_length);
text arr_name; integer array arr; integer arr_length;
begin integer ind;
outtext("Array "); outtext(arr_name);
outtext(" has "); outint(arr_length, 3); outtext(" elements:"); outimage;
for ind := 1 step 1 until arr_length do
outint(arr(ind), 10);
outimage;
end of write_array;
procedure unknown_test;
begin
outtext("Unknown test"); outimage;
Give_help;
end of unknown_test;
integer array A, B, C(1 : 100);
integer na, nb, nc;
Boolean more_testing;
more_testing := true;
while more_testing do
begin character c;
c := prompt_for_char("Which test do you want to perform? ");
if c = '?' then give_help else
if c = 'r' then read_array else
if c = 'm' then test_merge else
if c = 's' then test_Slowsort else
if c = 'x' then test_findminmax else
if c = 'w' then write_arrays else
if c = 'q' then more_testing := false
else unknown_test;
end;
end